package com.talkweb.newproject.admin.controller;

import com.talkweb.newproject.admin.rest.SysAclRest;
import com.talkweb.newproject.admin.sysacl.dto.SysAclDTO;
import com.talkweb.newproject.api.model.admin.SysAclParam;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**此接口为外部提供调用,通过注册中心获取相关业务接口标识,通过[feign]rest形式调用服务方接口
 * Created by qsf on 2019/5/7.
 */
@RestController
@RequestMapping(value="sysAcl-api")
@Slf4j
public class SysAclController {

    @Autowired
    private SysAclRest aclService;


    /**
     * 根据id获取业务对象
     * @param ID
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/querySysAclByPrimaryKey",method = RequestMethod.POST)
    public ResponseResult<SysAclDTO> querySysAclByPrimaryKey(@RequestParam("ID") String ID) throws Exception {
        ResponseResult<SysAclDTO> result = new ResponseResult<>();
        try{

            return aclService.querySysAclByPrimaryKey(ID);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 查询列表
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/getSysAclList",method = RequestMethod.POST)
    public ResponseResult<List<SysAclDTO>> querySysAclList(@RequestBody SysAclParam obj) throws Exception {
        ResponseResult<List<SysAclDTO>> result = new ResponseResult<>();
        try{

            return aclService.querySysAclList(obj);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 根据条件查询单个对象
     * @param dto
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method = RequestMethod.POST)
    public ResponseResult<SysAclDTO> queryLike(@RequestBody SysAclDTO dto) throws  Exception {
        ResponseResult<SysAclDTO> result = new ResponseResult<>();
        try{

            return aclService.queryLike(dto);
        }catch(Exception e) {
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 插入业务对象
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/insertSysAcl",method = RequestMethod.POST)
    public ResponseResult insertSysAcl(@RequestBody SysAclDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            return aclService.create(obj);
        }catch(Exception e) {
            log.error(e.getMessage());
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    /**
     * 更新数据
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/Web/update/v1",method=RequestMethod.POST)
    public ResponseResult updateSysAcl(@RequestBody SysAclDTO obj) throws Exception {
        ResponseResult result = new ResponseResult();
        try{

            return aclService.update(obj);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 删除数据
     * @param ids
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/Web/delete/v1",method = RequestMethod.POST)
    public ResponseResult deleteSycAcl(@RequestParam("ids") String ids) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            if(ids!= null && !"".equals(ids)) {
                aclService.deleteSysAcl(ids);
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功");
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败");
            }
            return result;
        }catch(Exception e) {
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }
}
